// 新代码
import { createRouter, createWebHistory } from 'vue-router'
import CommonData from '@/utils/commonData.js'

// createRouter 创建路由实例
// 配置 history模式
// 1. history模式： createWebHistory      地址栏不带#
// 2. hash模式：    createWebHashHistory  地址栏带 #
/*const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      // 路由自定义对象
      meta: {
        title: '首页'
      },
      component: () => import('@/views/layout/LayoutContainer.vue'),
      // 重定向到文章分类页面
      redirect: '/index',
      // 二级子路由
      children: [
        { path: '/index', name: 'index', meta: {title: '首页面板'}, component: () => import('@/views/index/index.vue') },
        { path: '/channel', name: 'channel', meta: {title: '文章分类'}, component: () => import('@/views/channel/index.vue') },
        { path: '/manage', name: 'manage', meta: {title: '文章管理'}, component: () => import('@/views/manage/index.vue') },
        { path: '/user', name: 'user', meta: {title: '基本资料'}, component: () => import('@/views/user/index.vue') },
        { path: '/avatar', name: 'avatar', meta: {title: '更换头像'}, component: () => import('@/views/avatar/index.vue') },
        { path: '/password', name: 'password', meta: {title: '重置密码'}, component: () => import('@/views/password/index.vue') },
        {
          path: '/member',
          name: 'member',
          meta: {title: '会员管理'},
          // component: () => import('@/views/member/index.vue'),
          redirect: '/member/create',
          children: [
            { path: '/member/create', name: 'create', meta: {title: '创建会员'}, component: () => import('@/views/member/create/index.vue') },
            { path: '/member/list', name: 'list', meta: {title: '会员列表'}, component: () => import('@/views/member/list/index.vue') }
          ]
        }
      ]
    }
  ]
})*/

// 动态路由配置
const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    // 路由自定义对象
    meta: {
      title: '首页'
    },
    component: () => import('@/views/layout/LayoutContainer.vue'),
    // 重定向到文章分类页面
    redirect: '/index',
    // 二级子路由
    /*children: [
      { path: '/index', name: 'index', meta: {title: '首页面板'}, component: () => import('@/views/index/index.vue') },
      { path: '/channel', name: 'channel', meta: {title: '文章分类'}, component: () => import('@/views/channel/index.vue') },
      { path: '/manage', name: 'manage', meta: {title: '文章管理'}, component: () => import('@/views/manage/index.vue') },
      { path: '/user', name: 'user', meta: {title: '基本资料'}, component: () => import('@/views/user/index.vue') },
      { path: '/avatar', name: 'avatar', meta: {title: '更换头像'}, component: () => import('@/views/avatar/index.vue') },
      { path: '/password', name: 'password', meta: {title: '重置密码'}, component: () => import('@/views/password/index.vue') },
      {
        path: '/member',
        name: 'member',
        meta: {title: '会员管理'},
        // component: () => import('@/views/member/index.vue'),
        redirect: '/member/create',
        children: [
          { path: '/member/create', name: 'create', meta: {title: '创建会员'}, component: () => import('@/views/member/create/index.vue') },
          { path: '/member/list', name: 'list', meta: {title: '会员列表'}, component: () => import('@/views/member/list/index.vue') }
        ]
      }
    ]*/
  }
]
// 1.筛选home
const homeRoutes = routes.filter(v => v.path === '/home')[0]
// 2.添加children
homeRoutes.children = []
let modules = import.meta.glob('../views/**/index.vue')

// 3.动态路由配置-递归
const initRoutes = (data, subObj) => {
  data.forEach(item => {
    /*homeRoutes.children.push({
      path: item.url,
      name: item.title,
      meta: {title: item.name},
      component: () => import(`../views/${item.component}/index.vue`)
    })*/
    const obj = {
      path: item.url,
      name: item.title,
      meta: {title: item.name},
      component: modules[`../views/${item.component}/index.vue`]
    }
    if(subObj instanceof Array) {
      subObj.push(obj)
    } else {
      if(!subObj.children) {
        subObj.children = []
      } else {
        subObj.children.push(obj)
      }
    }
    if(item.child) {
      initRoutes(item.child, obj)
    }
  })
}
initRoutes(CommonData, homeRoutes.children)

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router
